﻿//表单验证["ID|类型(或正则表达式、下拉值、后缀名等)|提示",...]
function Validator(aIds)
{
    var aLen = aIds.length;
    
    for(var i=0; i<aLen; i++)
    {
        var aId = aIds[i].split('|');
        var obj = document.getElementById(aId[0]);
        if(obj != null)
        {
            //获取控件类型
            var aType = obj.tagName.toLowerCase();
            if(aType == "input")
                aType = obj.type.toLowerCase();
            
            switch(aType)
            {
                case "text":
                    if(!checkTextBox(obj, aId[1], aId[2]))
                        return false;
                        break;
                case "textarea":
                    if(!checkTextBox(obj, aId[1], aId[2]))
                        return false;     
                    break;
                case "password":
                    if(!checkTextBox(obj, aId[1], aId[2]))
                        return false;     
                    break;
                case "select":
                    if(!checkDropDownList(obj, aId[1], aId[2]))
                        return false;
                    break;
               case "file":
                    if(!checkFileUpload(obj, aId[1], aId[2]))
                        return false;
                    break;
               default:
                    break;
            }
        }
    }
    
    return true;
}
//文本框
function checkTextBox(obj, type, message)
{
    var str1, str2 = message;
    switch(type.toLowerCase())
    {
        case "require":
            str1 = "";
            if(message == "")
                str2 = "该项必添！";
            break;
        case "nonull":
            str1 = /[ ]/g;
            if(message == "")
               str2 = "请输入数据(不能为空串)！";
            break;
        case "number":
            str1 = /^\d+$/;
            if(message == "")
               str2 = "请输入数字！";
            break;
        case "email":
            str1 = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            if(message == "")
                str2 = "请输入正确的邮件帐号！";
            break;
		case "english": 
			str1 = /^[A-Za-z]+$/;
			if(message = "")
				str2 = "请输入英文字符，不包含数字、下划线！";
			break;
        case "postcode":
            str1 = /^[1-9]\d{5}$/;
            if(message == "")
                str2 = "请输入正确的邮政编码！";
            break;
        case "username":
            str1 = /^[a-zA-Z0-9_-]{6,40}$/;
            if(message == "")
                str2 = "帐号格式不正确，必须由6至40个字母、数字、下划线或中横杠组成！";
            break;
        case "password":
            str1 = /^[a-zA-Z0-9_]{6,20}$/;
            if(message == "")
                str2 = "密码格式不正确，必须由6至20个字母、数字或下划线组成！";
            break;
        case "username_":
            str1 = /^[a-zA-Z0-9_-]{4,40}$/;
            if(message == "")
                str2 = "帐号格式不正确，必须由4至40个字母、数字、下划线或中横杠组成！";
            break;
        case "password_":
            str1 = /^[a-zA-Z0-9_]{4,20}$/;
            if(message == "")
                str2 = "密码格式不正确，必须由4至20个字母、数字或下划线组成！";
            break;
        case "website":
            str1 = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
            if(message == "")
                str2 = "请输入正确的网址！";
            break;
        case "ip":
            str1 = /\d+\.\d+\.\d+\.\d+/;
            if(message == "")
                str2 = "请输入正确的IP地址！";
            break;
        default:
            str1 = new RegExp(type, "g");    //自定义的正则表达式
            if(message == "")
                str2 = "请输入正确格式！";
           break;
    }
    
    if(type.toLowerCase() == "require")
    {
        if(obj.value == str1)
        {
            alert(str2);
            obj.focus();
            return false;
        }
    }
    else if(type.toLowerCase() == "nonull")
    {
        if(obj.value.replace(str1, "") == "")
        {
            alert(str2);
            obj.select();
            return false;
        }
    }
    else
    {
        if(obj.value.search(str1) == -1)
        {
            alert(str2);
            obj.select();
            return false;
        }
    }
    return true;
}
//下拉列表
function checkDropDownList(obj, type, message)
{
    if(obj.value == type)
    {
        var str = message;
        if(message == "")
            str = "请选择！";
        
        alert(str);
        obj.focus();
        return;
    }
    return true;
}
//上传文件
function checkFileUpload(obj, type, message)
{
    var str = message;
    if(message == "")
        str = "请上传文件！";
        
    if(type == "")
    {
        if(obj.value == "")
        {
            alert(str);
            obj.focus();
            return false;
        }
    }
    else
    {
        type = type.replace('*', '|');
    
        //判断是否允许不上传
        if(type.indexOf(':') != -1)
        {
            if(obj.value == "")
                return true;
            else
                type = type.replace(':', '');
        }
        
        type = "^(([a-zA-Z]:)|(\\\\{2}\\w+)\\$?)(\\\\(\\w[\\w ]*.*))+\\.(" + type + ")$";
        var re = new RegExp(type, "g");
        
        if(obj.value.toLowerCase().search(re) == -1)
        { 
            alert(str);
            obj.focus();
            return false;
        }
    }
    return true;
}